package view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import metier.DecouperFichier;

public class JDecoupage extends JFrame {
	
	JComboBox corpus = new JComboBox();
	JComboBox textes = new JComboBox();
	List<String> corpus_path, textes_path; 
	JButton valider;
	JButton fPhrase, fLigne,mPhrase,mLigne;
	
	DefaultTableModel model ;
    Object[][] datas;
	JTable matrice = new JTable();
	
	public JDecoupage() {
		super();
		init();
		corpus_path = new ArrayList<String>();
		// GRIDLAYOUT
        setLayout(new GridLayout(2,1));
        add(topPanel());
        model = (DefaultTableModel) matrice.getModel();
        JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	    pane.setViewportView(matrice);
	    matrice.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	    add(pane);
	    
	    File folder = new File("fichiersOriginaux");
	    for(File f : folder.listFiles()){
	    	if(!f.getName().equals(".DS_Store")){
	    		corpus.addItem(f.getName());
	    		corpus_path.add(f.getAbsolutePath());
	    	}
	    }
        
        setVisible(true);
	}
	
	
	
	public void init() {
		setTitle("Fouille De Textes - Decoupage");
	    setSize(new Dimension(470, 400));
	    setLocationRelativeTo(null);
	}
	
	public JPanel topPanel() {
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(3,1));
		//premiere ligne
		JPanel top = new JPanel();
		top.setLayout(new GridLayout(1,2));
		top.add(corpus);
		valider = new JButton("Valider");
		valider.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				textes.removeAllItems();
				textes_path = new ArrayList<String>();
				File folder = new File(corpus_path.get(corpus.getSelectedIndex()));
				File[] listOfFiles = folder.listFiles();
				for(File f : listOfFiles){
					if(f.isFile() && !f.getName().equals(".DS_Store")){
						textes.addItem(f.getName());
						textes_path.add(f.getAbsolutePath());
					}
				}
			}
				
		});
		top.add(valider);
		//deuxieme ligne
		JPanel middle = new JPanel();
		middle.setLayout(new GridLayout(2,1));
		middle.add(textes);
		//derniere ligne
		JPanel bottom = new JPanel();
		bottom.setLayout(new GridLayout(1,4));
		fPhrase = new JButton("Fichier - Phrase");
		fPhrase.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Model : "+model.getRowCount());
				for(int in=model.getRowCount()-1; in>=0;in--){
					model.removeRow(in);
				}
				List<String> res = DecouperFichier.decouperFichierPhrase(textes_path.get(textes.getSelectedIndex()));
				
				Object[] data = new Object[2];
				datas = new Object[res.size()][2];
				data[0] = "n° phrase";
				data[1] = "phrase";
				int index = 0, max = 0;
				model.setColumnIdentifiers(data);
				for(String phrase : res){
					max = max < phrase.length() ? phrase.length() : max ;
					datas[index][0] = "phrase "+index;
					datas[index][1] = res.get(index);
					
					model.addRow(datas[index]);
					++index;
				}
				
				matrice.getColumnModel().getColumn(1).setPreferredWidth(max*6);
				
				
			}
		});
		bottom.add(fPhrase);
		fLigne = new JButton("Fichier - Ligne");
		fLigne.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				for(int in=model.getRowCount()-1; in>=0;in--){
					model.removeRow(in);
				}
				List<String> res = DecouperFichier.decouperFichierLigne(textes_path.get(textes.getSelectedIndex()));
				System.out.println(res.size());
				
				Object[] data = new Object[2];
				datas = new Object[res.size()][2];
				data[0] = "n° ligne";
				data[1] = "ligne";
				int index = 0, max = 0;
				model.setColumnIdentifiers(data);
				for(String phrase : res){
					max = max < phrase.length() ? phrase.length() : max ;
					datas[index][0] = "ligne "+index;
					datas[index][1] = res.get(index);
					
					model.addRow(datas[index]);
					++index;
				}
				
				matrice.getColumnModel().getColumn(1).setPreferredWidth(max*6);
				
			}
		});
		bottom.add(fLigne);
		mPhrase = new JButton("Matrice mot/phrase");
		mPhrase.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				for(int in=model.getRowCount()-1; in>=0;in--){
					model.removeRow(in);
				}
				Map<String,Map<String, Integer>> res = DecouperFichier.comptageDecouperFichierPhrase(textes_path.get(textes.getSelectedIndex()));
				List<String> words = new ArrayList<String>();
				for(String key : res.keySet()){
					for(Entry<String, Integer> de : res.get(key).entrySet())
						words.add(de.getKey());
				}
				Object[] data = new Object[words.size()+1];
				data[0] = "phrase : ";
				int index = 1;
				for(String word : words){
					data[index++]=word;
				}
				datas = new Object[res.size()+1][words.size()];
				model.setColumnIdentifiers(data);
				index = 0;
				for(String key : res.keySet()){
					datas[index][0] = key;
					for(Entry<String, Integer> de : res.get(key).entrySet()){
						int i = words.indexOf(de.getKey())+1;
						datas[index][i]=de.getValue(); 
					}
					for(int i = 0; i < words.size();i++){
						if(datas[index][i] == null)
							datas[index][i]=0;
						
					}
				model.addRow(datas[index]);
				index++;

				}
			}
		});
		bottom.add(mPhrase);
		mLigne = new JButton("Matrice mot/ligne");
		mLigne.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				for(int in=model.getRowCount()-1; in>=0;in--){
					model.removeRow(in);
				}
				Map<String,Map<String, Integer>> res = DecouperFichier.comptageDecouperFichierLigne(textes_path.get(textes.getSelectedIndex()));
				List<String> words = new ArrayList<String>();
				for(String key : res.keySet()){
					for(Entry<String, Integer> de : res.get(key).entrySet())
						words.add(de.getKey());
				}
				Object[] data = new Object[words.size()+1];
				data[0] = "ligne : ";
				int index = 1;
				for(String word : words){
					data[index++]=word;
				}
				datas = new Object[res.size()+1][words.size()];
				model.setColumnIdentifiers(data);
				index = 0;
				for(String key : res.keySet()){
					datas[index][0] = key;
					for(Entry<String, Integer> de : res.get(key).entrySet()){
						int i = words.indexOf(de.getKey());
						datas[index][i]=de.getValue(); 
					}
					for(int i = 0; i < words.size();i++){
						if(datas[index][i] == null)
							datas[index][i]=0;
						
					}
				model.addRow(datas[index]);
				index++;

				}
				
			}
		});
		bottom.add(mLigne);
		
		//ajouter les 3 Jpanel au résultat
		
		res.add(top);
		res.add(middle);
		res.add(bottom);
		
		return res;
	}

}
